<template>
  <div class="pho-form-item" :style="newStyle">
    <van-field
      class="form-input-hidden"
      v-bind="$attrs"
      :name="$attrs.__vModel__"
      :value="selected.length ? value : ''"
      :label="$attrs.showLabel ? $attrs.label : ''"
      input-align="right"
      readonly
      right-icon="plus"
    >
    <template #label>
      <span v-if="$attrs.showLabel" slot="label" :style="newStyle.font">{{$attrs.label}}</span>
    </template>
    </van-field>
    <div v-if="!$attrs.onlyShow" class="user-picker-add form-placeholder" @click="handleClick">
      {{ $attrs.placeholder }}
      <van-icon name="plus" />
    </div>
    <!-- 展示栏 -->
    <NameShow
      v-if="selected.length"
      style="padding: 0 12px 12px 12px"
      v-model="selected"
      :props="dataObj.props"
      :onlyShow="$attrs.onlyShow"
      :readonly="$attrs.readonly"
      @change="callBack"
    />
    <OrgPicker
      ref="OrgPicker"
      :data="dataObj"
      :multiple="$attrs.multiple"
      type="dept"
      :search="search"
      @callBack="callBack"
    />
  </div>
</template>

<script>
import OrgPicker from "../components/orgPicker.vue";
import NameShow from "../components/nameShow.vue";
import { comMixin } from "../../../utils/comMixins";
export default {
  mixins: [comMixin],
  name: "phone-form-org-picker",
  components: {
    OrgPicker,
    NameShow,
  },
  model: {
    prop: "value", // 绑定的值，通过父组件传递
    event: "update",
  },
  props: {
    // 绑定的输入值
    value: {
      type: [String],
      default: "",
    },
    props: {
      type: Object,
      default: () => {},
    },
    // 搜索配置
    search: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      selected: JSON.parse(this.values || "[]"),
    };
  },

  watch: {
    value: {
      immediate: true,
      deep: true,
      handler(newValue, oldValue) {
        this.selected = JSON.parse(newValue || "[]");
      },
    },
  },
  methods: {
    handleClick() {
      if (this.$attrs.disabled || this.$attrs.readonly) return;
      this.$refs.OrgPicker.open(this.selected);
    },
    callBack(value) {
      const newValue = value.length ? JSON.stringify(value) : null;
      this.$emit("update", newValue);
      this.$emit("confirm", newValue);
      this.onConfirm(newValue);
    },
  },
};
</script>

<style lang="scss" scoped>
.user-picker-add {
  padding: 12px 16px;
  padding-top: 0;
  cursor: pointer;
  font-size: 13px;
}
</style>
